private ["_HALOunit", "_HALOunit_pitch", "_i1", "_i2", "_vel", "_dir"];

_HALOunit = _this select 0;
diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"HALOHandle.sqf","Input", _this];

BIS_HALO_unit_dir = 0;
BIS_HALO_unit_speed = 10;
BIS_HALO_turnR = 0;

_HALOunit_pitch = -90;
_i1 = 50;
_i2 = 0;

playsound "DPara";
_HALOunit switchMove "HaloFreeFall_non";

_open_chute_action = _HALOunit addAction [localize "STR_HALO_OPEN_CHUTE", "scripts\HALO\HALOParachute.sqf", "", 20, true, true, "Eject", "true"];

if (isNil "halokeyspressed") then {
	halokeyspressed = {
		_key = _this select 1;
		if (_key in (actionKeys "TurnLeft")) then {[] spawn freefall_ChangedirL};
		if (_key in (actionKeys "TurnRight")) then {[] spawn freefall_ChangedirR};
		if (_key in (actionKeys "MoveForward")) then {BIS_HALO_unit_speed = BIS_HALO_unit_speed + 0.1};
		if (_key in (actionKeys "MoveBack")) then {BIS_HALO_unit_speed = BIS_HALO_unit_speed - 0.1};
	};
};
if (isNil "freefall_ChangedirL") then {
	freefall_ChangedirL ={
		for "_i" from 0 to 44 do {
			BIS_HALO_unit_dir = BIS_HALO_unit_dir - 0.02;
			sleep 0.05;
		};
	};
};
if (isNil "freefall_ChangedirR") then {
	freefall_ChangedirR ={
		for "_i" from 0 to 44 do {
			BIS_HALO_unit_dir = BIS_HALO_unit_dir + 0.02;
			sleep 0.05;
		};	
	};
};

d_halo_keyhandler = (findDisplay 46) displayAddEventHandler ["KeyDown","_this call halokeyspressed"];

_HALOunit switchMove "HaloFreeFall_non";

while {alive _HALOunit && (vehicle _HALOunit == _HALOunit) && (position player select 2 >= 2)} do {
	_i1 = _i1 + 1;
	_i2 = _i2 + 1;

	_HALOunit setdir BIS_HALO_unit_dir;

	_vel = velocity _HALOunit;
	_dir = direction _HALOunit;
	_HALOunit setVelocity [(sin _dir * BIS_HALO_unit_speed), (cos _dir * BIS_HALO_unit_speed), (_vel select 2)];

	if (BIS_HALO_unit_speed > 20 && _i2 > 80) then {_HALOunit playMove "HaloFreeFall_F"; _i2 = 0};
	if (BIS_HALO_unit_speed < 20 && _i2 > 250) then {_HALOunit playMove "HaloFreeFall_non"; _i2 = 0};

	switch (true) do {
		case (BIS_HALO_unit_speed > 50): {BIS_HALO_unit_speed = 50};
		case (BIS_HALO_unit_speed < 2): {BIS_HALO_unit_speed = 2};
	};

	if (_i1 > 70) then {playsound "DPara"; _i1 = 0};

	hintsilent format [localize "STR_ALTITUDE", (round (getpos _HALOunit select 2)), round(speed _HALOUnit)];

	if (position _HALOunit select 2 < 2) exitWith {_HALOunit playmove ""; _HALOunit switchmove ""; _HALOunit setvelocity [0,0,0]; _HALOunit setdammage 1};
	if (vehicle _HALOunit != _HALOunit) exitWith {};
	sleep 0.01;
};

if (!isNil "d_halo_keyhandler" && !alive _HALOunit) then {(findDisplay 46) displayRemoveEventHandler ["KeyDown",d_halo_keyhandler];d_halo_keyhandler = nil};
